package com.leo.common.security.server.handler;

import com.leo.common.core.constant.CacheConstants;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.client.JdbcClientDetailsService;

import javax.sql.DataSource;

/**
 * <p>
 * A service that provides the details about an OAuth2 client.
 * </p>
 *
 * @author ：Leo
 * @since ：2021-04-06 12:00
 */
public class ClientDetailsService extends JdbcClientDetailsService {

	public ClientDetailsService(DataSource dataSource) {
		super(dataSource);
	}

	/**
	 * 重写原生方法，支持Redis缓存
	 * @param clientId 客户端Id
	 * @return ClientDetails
	 * @throws InvalidClientException 异常
	 */
	@Override
	@Cacheable(value = CacheConstants.GLOBALLY + CacheConstants.CLIENT_DETAILS_KEY, key = "#p0",
			unless = "#result == null")
	public ClientDetails loadClientByClientId(String clientId) throws InvalidClientException {
		return super.loadClientByClientId(clientId);
	}

}
